package com.nemodynamic.cattivial.gamecenter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.nemodynamic.cattivial.gamecenter.beans.LiderBoardScore;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class LiderBoardUtils {
	public static final String BEST_SCORE_OBJ = "BestScore";
	public static final String LIDERBOARD_NAME = "liderBoardName";
	public static final String GAMER_ID = "gamerID";
	public static final String GAMER_NAME = "gamerName";
	public static final String MATCH_TYPE = "matchType";
	public static final String SCORE = "score";

	public static void setScore(String liderBoardName, String matchType, String gamerID, String gamerName, int score) {
		ParseObject gameScore = new ParseObject(BEST_SCORE_OBJ);
		gameScore.put(LIDERBOARD_NAME, liderBoardName);
		gameScore.put(GAMER_ID, gamerID);
		gameScore.put(GAMER_NAME, gamerName);
		gameScore.put("MATCH_TYPE", matchType);
		gameScore.put(SCORE, score);
		gameScore.saveInBackground();
	}

	public static List<LiderBoardScore> getScore(String liderBoardName, String matchType, int maxEntry) {
		try {
			List<LiderBoardScore> liderBoardScores = new ArrayList<LiderBoardScore>();
			ParseQuery query = new ParseQuery(BEST_SCORE_OBJ);
			query.whereEqualTo(MATCH_TYPE, matchType);
			query.whereEqualTo(LIDERBOARD_NAME, liderBoardName);
			query.addDescendingOrder(SCORE);
			query.setLimit(maxEntry);
			List<ParseObject> scoreList = query.find();
			if(scoreList==null || scoreList.isEmpty()){
				return null;
			}else{
				for (ParseObject liderBoardScore : scoreList) {
					liderBoardScores.add(new  LiderBoardScore(liderBoardScore.getString(LIDERBOARD_NAME),
															  liderBoardScore.getString(GAMER_ID), 
															  liderBoardScore.getString(GAMER_NAME), 
															  liderBoardScore.getString(matchType), 
															  liderBoardScore.getInt(SCORE)));
				}
				return liderBoardScores;
			}
			
		} catch (ParseException e) {
			return null;
		}

	}
}
